<?php
class AdminKqMiennam extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'ketqua_miennam';
    }

    // nhan cac thuong tinh 
    public function attributeLabels(){} 

    public function getCountByProvince($province_id,$begin="",$end="") {
        $row = array();
        if($begin=="" && $end==""){
            $sql = "SELECT count(*) as count
            FROM ketqua_miennam WHERE province_id =".$province_id;
        } else{
            if($begin<=$end)
            {
                $sql = "SELECT count(*) as count 
                FROM ketqua_miennam WHERE ngay_quay >= '".$begin."' AND ngay_quay <= '".$end."' AND province_id =".$province_id;
            } else{
                $sql = " id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user
                FROM ketqua_miennam WHERE ngay_quay >= '".$end."' AND ngay_quay <= '".$begin."' AND province_id =".$province_id;
            }
        }
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }

    public function getRowByDateAndProvince($id_province,$date=''){
        $day_now = date("Y-m-d",time());
        $id_province = intval($id_province);

        $row = array();

        if($date !=""){
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,
            giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
            giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam
            FROM ketqua_miennam 
            WHERE ngay_quay='".$date."' AND province_id=".$id_province;
        }else{
            $sql = "SELECT id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,
            giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
            giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam
            FROM ketqua_miennam 
            WHERE ngay_quay <> '".$day_now."' AND province_id=".$id_province." ORDER by ngay_quay DESC LIMIT 0,1";    
        } 
        //echo $sql;die; 
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();

        return $row;
    }

    public function getRowByDate($date){

        $row = array();
        $sql = "SELECT ketqua_miennam.id,province_id,DATE_FORMAT(ketqua_miennam.ngay_quay,'%d/%m/%Y') as ngay_quay,giai_dacbiet,giai_nhat,
        giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
        giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam,province.name
        FROM ketqua_miennam 
        LEFT JOIN province ON province.id = ketqua_miennam.province_id
        WHERE ngay_quay='".$date."'";
        //echo $sql;die;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();

        return $row;
    }

    public function getRowByDateAndArrayProvince($arr_id,$date=''){
        $id_array = implode(",",$arr_id);
        $day_now = date("Y-m-d",time());  

        $rows = array();


        if($date ==""){
            $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,
            giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
            giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam
            FROM ketqua_miennam WHERE ngay_quay <> '".$day_now."' AND province_id IN (".$id_array.")  GROUP BY province_id ORDER BY ngay_quay DESC";
            echo $sql;die;
        }else{
            $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay_thuong,giai_dacbiet,giai_nhat,
            giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
            giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam
            FROM ketqua_miennam WHERE ngay_quay='".$date."' AND province_id IN (".$id_array.")";
        }
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();

        return $rows;
    }  

    public function getRowInDateAndProvince($province_id,$first,$row_per_page,$begin="",$end=""){
        $day_now = date("Y-m-d",time());
        $rows = array();
        if($begin=="" && $end==""){
            $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
            FROM ketqua_miennam WHERE province_id=".$province_id." ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
        } else{
            if($begin<=$end)
            {
                $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
                FROM ketqua_miennam WHERE ngay_quay >='".$begin."' AND ngay_quay <'".$end."' AND province_id=".$province_id." ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            } else{ 
                $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,create_user,modify_user
                FROM ketqua_miennam WHERE ngay_quay >='".$end."' AND ngay_quay <'".$begin."' AND province_id=".$province_id." ORDER BY id DESC LIMIT ".$first.",".$row_per_page."";
            }
        }  
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $rows = $command->queryAll();
        return $rows;
    }
    
    public function getRowById($id){
        $row = array();
        $sql = "SELECT id,province_id,DATE_FORMAT(ngay_quay,'%d/%m/%Y') as ngay_quay,giai_dacbiet,giai_nhat,
            giai_nhi,giai_ba_1,giai_ba_2,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,
            giai_nam,giai_sau_1,giai_sau_2,giai_sau_3,giai_bay,giai_tam
            FROM ketqua_miennam WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow(); 
        return $row;   
    }
    
    public function insertThongke($ketqua_id,$boso,$ngay_quay,$giai,$create_user){
        $kqmienbac = new AdminKqMienbac();
        $last_id = 0;
        $timestamp = strtotime($ngay_quay);
        $day = getdate($timestamp);
        $thu = StringUtils::getDayVi($day["wday"]);
        
        $dau = substr($boso,0,1);
        $cuoi = substr($boso,1,1);
        
        $dau = intval($dau);
        $cuoi = intval($cuoi);
        
        $is_dacbiet = $giai=="GDB" ? 1:0;
        $is_tongchan = ($dau+$cuoi)%2 ==0 ? 1:0;
        $is_tongle = ($dau+$cuoi)%2 !=0 ? 1:0;
        $is_bochanle = ($dau%2==0 && $cuoi%2!=0)? 1 :0;
        $is_bolechan = ($dau%2!=0 && $cuoi%2==0)? 1 :0;
        $is_bochanchan = ($dau%2==0 && $cuoi%2==0)? 1 :0;
        $is_bolele = ($dau%2!=0 && $cuoi%2!=0)? 1 :0;
        $is_bokep = $dau==$cuoi ? 1:0;
        $is_bosatkep = 0;
        //echo var_dump($kqmienbac->arrSK);die;
        foreach($kqmienbac->arrSK as $value){
            if($boso == $value) {$is_bosatkep=1;break;}
        }
        
        $tan_so = 1;
        $tong = $dau + $cuoi;
        $tong_bo = substr($tong,1,1);
        
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $last_id = 0;
        $sql = "INSERT INTO thongke_loto_miennam(ketqua_id,province_id,boso,thu,
             is_dacbiet,is_tongchan,is_tongle,is_bochanle,is_bolechan,is_bochanchan,is_bolele,is_bokep,
             is_bosatkep,tan_so,dau_so,dit_so,tong_bo,giai,ngay_quay,create_date,create_user)
             VALUES (:ketqua_id,:province_id,:boso,:thu,:is_dacbiet,:is_tongchan,:is_tongle,
             :is_bochanle,:is_bolechan,:is_bochanchan,:is_bolele,:is_bokep,:is_bosatkep,:tan_so,:dau_so,:dit_so,
             :tong_bo,:giai,:ngay_quay,:create_date,:create_user)";
            
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":ketqua_id", $ketqua_id, PDO::PARAM_INT );
        $command->bindValue ( ":province_id", 0, PDO::PARAM_INT );
        $command->bindValue ( ":boso", $boso, PDO::PARAM_STR );
        $command->bindValue ( ":thu", $thu, PDO::PARAM_INT );
        $command->bindValue ( ":is_dacbiet", $is_dacbiet, PDO::PARAM_INT );
        $command->bindValue ( ":is_tongchan", $is_tongchan, PDO::PARAM_INT );
        $command->bindValue ( ":is_tongle", $is_tongle, PDO::PARAM_INT );
        $command->bindValue ( ":is_bochanle", $is_bochanle, PDO::PARAM_INT );
        $command->bindValue ( ":is_bolechan", $is_bolechan, PDO::PARAM_INT );
        $command->bindValue ( ":is_bochanchan", $is_bochanchan, PDO::PARAM_INT );
        $command->bindValue ( ":is_bolele", $is_bolele, PDO::PARAM_INT );
        $command->bindValue ( ":is_bokep", $is_bokep, PDO::PARAM_INT );
        $command->bindValue ( ":is_bosatkep", $is_bosatkep, PDO::PARAM_INT );     
        $command->bindValue ( ":tan_so", $tan_so, PDO::PARAM_INT );
        $command->bindValue ( ":dau_so", $dau, PDO::PARAM_INT );
        $command->bindValue ( ":dit_so", $cuoi, PDO::PARAM_INT );
        $command->bindValue ( ":tong_bo", $tong_bo, PDO::PARAM_INT ); 
        $command->bindValue ( ":giai", $giai, PDO::PARAM_STR ); 
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        //echo $sql;die;
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        //echo $last_id;die;
        return $last_id;
    }
    public function getIdByBoso($boso,$ketqua_id){
        $connect = Yii::app()->db;
        $sql = "SELECT id FROM thongke_loto_miennam WHERE boso=".$boso." AND ketqua_id = ".$ketqua_id;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
    }
    
    public function updateTansoThongKe($id){
        $connect = Yii::app()->db;
        $sql = "UPDATE thongke_loto_miennam SET `tanso` = `tanso` +1 WHERE id=".$id;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    } 
    
    public function insertGiaiTam($province_id,$giai_tam,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_tam,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_tam, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_tam", $giai_tam, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiTam($id,$giai_tam,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_tam = :giai_tam, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_tam", $giai_tam, PDO::PARAM_STR );        
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    } 
    
    public function insertGiaiBay($province_id,$giai_bay,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_bay,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_bay, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_bay", $giai_bay, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiBay($id,$giai_bay,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_bay = :giai_bay, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_bay", $giai_bay, PDO::PARAM_STR );        
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }
    
    public function insertGiaiSau($province_id,$giai_sau_1,$giai_sau_2,$giai_sau_3,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_sau_1,giai_sau_2,giai_sau_3,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_sau_1, :giai_sau_2, :giai_sau_3, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_sau_1", $giai_sau_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_2", $giai_sau_2, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_3", $giai_sau_3, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiSau($id,$giai_sau_1,$giai_sau_2,$giai_sau_3,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_sau_1 = :giai_sau_1,giai_sau_2 = :giai_sau_2,giai_sau_3 = :giai_sau_3, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_sau_1", $giai_sau_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_2", $giai_sau_2, PDO::PARAM_STR );
        $command->bindValue ( ":giai_sau_3", $giai_sau_3, PDO::PARAM_STR );        
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }
    
    public function insertGiaiNam($province_id,$giai_nam,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_nam,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_nam, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_nam", $giai_nam, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiNam($id,$giai_nam,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_nam = :giai_nam, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nam", $giai_nam, PDO::PARAM_STR );        
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }
    
    public function insertGiaiTu($province_id,$giai_tu_1,$giai_tu_2,$giai_tu_3,$giai_tu_4,$giai_tu_5,$giai_tu_6,$giai_tu_7,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_tu_1,giai_tu_2,giai_tu_3,giai_tu_4,giai_tu_5,giai_tu_6,giai_tu_7,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_tu_1, :giai_tu_2, :giai_tu_3,:giai_tu_4, :giai_tu_5, :giai_tu_6, :giai_tu_7, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_tu_1", $giai_tu_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_2", $giai_tu_2, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_3", $giai_tu_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_4", $giai_tu_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_5", $giai_tu_5, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_6", $giai_tu_6, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_7", $giai_tu_7, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiTu($id,$giai_tu_1,$giai_tu_2,$giai_tu_3,$giai_tu_4,$giai_tu_5,$giai_tu_6,$giai_tu_7,$modify_user){
        //echo $id;die;
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_tu_1 = :giai_tu_1,giai_tu_2 = :giai_tu_2,giai_tu_3 = :giai_tu_3, 
        giai_tu_4 = :giai_tu_4,giai_tu_5 = :giai_tu_5,giai_tu_6 = :giai_tu_6,giai_tu_7 = :giai_tu_7,
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_tu_1", $giai_tu_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_2", $giai_tu_2, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_3", $giai_tu_3, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_4", $giai_tu_4, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_5", $giai_tu_5, PDO::PARAM_STR );
        $command->bindValue ( ":giai_tu_6", $giai_tu_6, PDO::PARAM_STR ); 
        $command->bindValue ( ":giai_tu_7", $giai_tu_7, PDO::PARAM_STR );       
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        //echo $result;die;
        return $result;
    } 
    
    public function insertGiaiBa($province_id,$giai_ba_1,$giai_ba_2,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_ba_1,giai_ba_2,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_ba_1, :giai_ba_2, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_ba_1", $giai_ba_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_2", $giai_ba_2, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiBa($id,$giai_ba_1,$giai_ba_2,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_ba_1 = :giai_ba_1,giai_ba_2 = :giai_ba_2, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_ba_1", $giai_ba_1, PDO::PARAM_STR );
        $command->bindValue ( ":giai_ba_2", $giai_ba_2, PDO::PARAM_STR );
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }    
    
    public function insertGiaiNhi($province_id,$giai_nhi,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_nhi,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_nhi, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_nhi", $giai_nhi, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiNhi($id,$giai_nhi,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_nhi = :giai_nhi, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhi", $giai_nhi, PDO::PARAM_STR );
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }    
    
    public function insertGiaiNhat($province_id,$giai_nhat,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_nhat,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_nhat, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_nhat", $giai_nhat, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiNhat($id,$giai_nhat,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_nhat = :giai_nhat, 
        modify_user = :modify_user, modify_date = :modify_date WHERE id = :id";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":giai_nhat", $giai_nhat, PDO::PARAM_STR );
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );
        $result = $command->execute();
        
        return $result;
    }   
    
    public function insertGiaiDacbiet($province_id,$giai_dacbiet,$ngay_quay,$create_user){
        $last_id = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "INSERT INTO ketqua_miennam(province_id,giai_dacbiet,ngay_quay,create_user,create_date) 
                VALUES (:province_id,:giai_dacbiet, :ngay_quay, :create_user, :create_date)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":province_id", $province_id, PDO::PARAM_INT );
        $command->bindValue ( ":giai_dacbiet", $giai_dacbiet, PDO::PARAM_STR );
        $command->bindValue ( ":ngay_quay", $ngay_quay, PDO::PARAM_STR );
        $command->bindValue ( ":create_user", $create_user, PDO::PARAM_STR );
        $command->bindValue ( ":create_date", $date, PDO::PARAM_STR );
        $result = $command->execute(); 
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    } 
    public function updateGiaiDacbiet($id,$giai_dacbiet,$modify_user){
        $result = 0;
        $date = date("Y-m-d H:i:s",time());
        $connect = Yii::app()->db;
        $sql = "UPDATE ketqua_miennam SET giai_dacbiet = '$giai_dacbiet', 
        modify_user = '$modify_user', modify_date = '$date' WHERE id = $id";
        //echo $sql;die;
        $command = $connect->createCommand ($sql);
/*        $command->bindValue ( ":giai_dacbiet", $giai_dacbiet, PDO::PARAM_STR );
        $command->bindValue ( ":modify_user", $modify_user, PDO::PARAM_STR );
        $command->bindValue ( ":modify_date", $date, PDO::PARAM_STR );
        $command->bindValue ( ":id", $id, PDO::PARAM_INT );*/
        $result = $command->execute();
        
        return $result;
    }   
    
    public function deleteRowById($id){
        $sql = "DELETE FROM ketqua_miennam WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;
    }
}
